package com.atguigu.springcloud.config;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;

/**
 * result类型
 * @param <T>
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {

    public Result(boolean success, int code) {
        this.setStatus(success);
        this.setCode(code);
    }

    public Result(boolean success, int code, T data) {
        this.setStatus(success);
        this.setCode(code);
        this.setData(data);
    }

    public Result(boolean success, int code, String msg) {
        this.setStatus(success);
        this.setCode(code);
        this.setMsg(msg);
    }


    /**
     * 请求是否成功
     * true:成功
     * false：失败
     */
    private boolean status;

    /**
     * 状态码
     * 成功：200
     * 失败：其他
     */
    private int code;

    /**
     * 失败状态码描述
     * 如果成功不返回
     * 失败返回状态码对应的msg消息
     */
    private String msg;

    /**
     * 请求数据的结果
     */
    private T data;


    public static <T> Result<T> success(List<Map<String, Object>> maps) {
        final int code = 200;
        return new Result<T>(true, code);
    }

    public static <T> Result<T> success(String msg) {
        final int code = 200;
        return new Result<T>(true, code, msg);
    }

    public static <T> Result<T> success(HttpStatusEnum httpStatusEnum) {
        return new Result<T>(false, httpStatusEnum.code(), httpStatusEnum.reasonPhraseUS());
    }

    public static <T> Result<T> success(T data) {
        final int code = 200;
        return new Result<T>(true, code, data);
    }

    public static <T> Result<T> success(T data, String msg) {
        final int code = 200;
        return new Result<T>(true, code, msg, data);
    }

    public static <T> Result<T> fail(HttpStatusEnum httpStatusEnum) {
        return new Result<T>(false, httpStatusEnum.code(), httpStatusEnum.reasonPhraseUS());
    }

    public static <T> Result<T> fail(String msg) {
        final int code = 500;
        return new Result<T>(true, code, msg);
    }


    public static <T> Result<T> fail(HttpStatusEnum httpStatusEnum, String msg) {
        return new Result<T>(false, httpStatusEnum.code(), msg);
    }


}
